﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MPNotificationBar.Generals.Controls
{
    public class InternetAccess
    {
        const int _windowID = 0; // WindowID of windowplugin belonging to this setup. Enter your own unique code
        System.Timers.Timer t = new System.Timers.Timer(15000);
        MPNotificationBar.INotification _Notification;

        [System.Runtime.InteropServices.DllImport("wininet.dll")]
        private extern static bool InternetGetConnectedState(out int Description, int ReservedValue);

        public InternetAccess()
        {
            t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
            t.Enabled = true;
            t.Start();
            MPNotificationBar.NotificationBarManager.OnNotificationRemoved += new MPNotificationBar.NotificationBarManager.NotificationRemoved(NotificationBarManager_OnNotificationRemoved);
        }

        void NotificationBarManager_OnNotificationRemoved(MPNotificationBar.INotification iNotification)
        {
            if (_Notification == iNotification)
                _Notification = null;
        }

        void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (!IsConnectedToInternet())
            {
                if (_Notification == null)
                    _Notification = MPNotificationBar.NotificationBarManager.AddNotification(_windowID, "No internet access", MPNotificationBar.NotificationBarManager.Types.Warning);
            }
            else
            {
                if (_Notification != null)
                    _Notification.Remove = true;
            }
        }

        public static bool IsConnectedToInternet()
        {
            int Desc;
            return InternetGetConnectedState(out Desc, 0);
        }
    }
}
